package cn.xydpeng.springframework.aop.framework;

import cn.xydpeng.springframework.aop.SpringProxy;

/**
 * @author xydpeng
 * @version v1.0
 * @date: 2022/10/31
 * @description:
 */
public class DefaultAopProxyFactory implements AopProxyFactory {


    @Override
    public AopProxy createAopProxy(AdvisedSupport config) throws RuntimeException {
        if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {

            return new ObjenesisCglibAopProxy(config);
        } else {
            return new JdkDynamicAopProxy(config);
        }
    }

    private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
        Class<?>[] ifcs = config.getProxiedInterfaces();
        return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0])));
    }


}
